package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.InformationActivity;
import com.kkd.kkdapp.adapter.PublicInfoAdapter;
import com.kkd.kkdapp.db.user.model.KkdMessageModel;
import com.kkd.kkdapp.http.entity.InformationData;
import com.kkd.kkdapp.http.entity.InformationDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/**
 * 系统消息
 * 
 * @author mty
 * @rebuild by zhaochuang 20160525
 */
public class PublicInfoFragment extends Fragment implements KKDScrollListener {

	private final int CODE_ZRRO = 1;
	private final int CODE_ELSE = 2;
	private InformationActivity mInformationActivity;
	private KkdXlistView manage_lv;
	private ImageView img_null;
	private TextView tv_null;
	private PublicInfoAdapter mAdapter;
	public int pageNO = 1;
	private int pageSize = 10;
	private boolean REFRESHFLAG = true;
	private List<InformationDataList> dataList;
	private Handler handler = new Handler();
	private ListView mListView;
	private Dialog loadDialog;
	private boolean firstEntryFlag = true; // 第一次进入做界面异常判断
	private RelativeLayout rl_null;
	private InformationData mInformationData;
	private int mPageNo;
	private int mPageCount;
	private List<InformationDataList> mList = new ArrayList<InformationDataList>();

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZRRO:
				if (loadDialog != null) {
					loadDialog.dismiss();
				}

				if (mPageNo > mPageCount) {
					manage_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					if (mPageNo == 1) {
						mList.clear();
					}
					dataList = mInformationData.getDataList();
					if (mAdapter != null) {
						mList.addAll(dataList);
						mAdapter.notifyDataSetChanged();
					}
					if (dataList.size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
				}

				manage_lv.stopRefresh();
				manage_lv.setNoMore(false);
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				manage_lv.setRefreshTime(time);
				LogUtil.e("dateList", "" + dataList);
				firstEntryFlag = true;
				if (REFRESHFLAG) {
					if (dataList == null || dataList.size() == 0) {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								mInformationActivity, firstEntryFlag, true,
								rl_null, img_null, tv_null, "暂无消息~",
								R.drawable.img_null);
					} else {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								mInformationActivity, firstEntryFlag, false,
								rl_null, img_null, tv_null, "", 0);
					}
				}
				break;
			case CODE_ELSE:
				int statusCode = (Integer) msg.obj;
				if (statusCode == KkdHttpClient.NO_NET) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							mInformationActivity, firstEntryFlag, true,
							rl_null, img_null, tv_null, "网络错误，请稍后重试",
							R.drawable.img_null_net);
				} else if (statusCode == KkdHttpClient.RESOLUTION_EXCEPTION) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							mInformationActivity, firstEntryFlag, true,
							rl_null, img_null, tv_null, "服务异常，请稍后重试",
							R.drawable.img_null_net);
				}

				manage_lv.stopLoadMore(1);
				manage_lv.stopRefresh();
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				break;
			}
		};
	};

	/***
	 * 空界面处理
	 */
	public void uiNullDispose(boolean uiNullFlag, String nullStr, int imgID) {
		if (uiNullFlag) {
			if (firstEntryFlag) {
				rl_null.setVisibility(View.VISIBLE);
				img_null.setImageResource(imgID);
				tv_null.setText(nullStr); // "暂无消息~"
				firstEntryFlag = false;
			} else {
				if (!rl_null.isShown()) {
					showToast(mInformationActivity, nullStr);
				}
			}
		} else {
			rl_null.setVisibility(View.GONE);
			firstEntryFlag = false;
		}
	}

	public static PublicInfoFragment newInstance() {
		PublicInfoFragment fragment = new PublicInfoFragment();
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mInformationActivity = (InformationActivity) activity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.intender_fragment, null);
		manage_lv = (KkdXlistView) view.findViewById(R.id.manage_lv);
		img_null = (ImageView) view.findViewById(R.id.img_null);
		tv_null = (TextView) view.findViewById(R.id.tv_null);
		rl_null = (RelativeLayout) view.findViewById(R.id.rl_null);

		manage_lv.setKKDScrollListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		mAdapter = new PublicInfoAdapter(mInformationActivity, mList);
		mListView.setAdapter(mAdapter);
		manage_lv.startRefresh();
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		queryNotificationMsgList();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}

	/**
	 * 系统消息
	 */
	public void queryNotificationMsgList() {

		KkdHttpMagager.getInstance().getSystemNewsManager(pageNO, pageSize,
				InformationData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mInformationData = (InformationData) responseBody;
						mPageNo = mInformationData.getPageNO();
						mPageCount = mInformationData.getPageCount();
						mHandler.sendEmptyMessage(CODE_ZRRO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = statusCode;
						msg.what = CODE_ELSE;
						mHandler.sendMessage(msg);
					}
				});
	}

	public long getSystemcreateTime() {
		if (dataList != null) {
			return dataList.get(0).getCreateTime();
		}
		return -1;
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		queryNotificationMsgList();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		queryNotificationMsgList();
		REFRESHFLAG = false;
	}

	public void setIsToAllReaded() {
		KkdMessageModel.setAllReaded();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils
				.toLoadDialog(mInformationActivity, "正在更新数据...");
	}

	public void showToast(Activity acitivity, String hintLanguage) {
		AppUtil.makeToastForCustomer(acitivity, hintLanguage);
	}

	public void showToast(int hintLanguage) {
		AppUtil.makeToastForCustomer(getActivity(), hintLanguage);
	}
}
